package org.malred.hotelsystem.fragment.admin;

import android.widget.Button;
import android.widget.EditText;

import org.malred.hotelsystem.R;
import org.malred.hotelsystem.activities.BaseActivity;
import org.malred.hotelsystem.activities.admin.ManagerActivity;
import org.malred.hotelsystem.dao.HotelDao;
import org.malred.hotelsystem.fragment.BaseFragment;
import org.malred.hotelsystem.model.entity.HotelEntity;

import java.util.UUID;

/**
 * 修改或添加的页面
 */
public class ManageAddFragment extends BaseFragment {
    EditText etTitle;
    EditText etDesc;
    EditText etNum;
    Button subBtn;

    @Override
    protected int setLayoutResourceID() {
        return R.layout.fragment_add;
    }

    @Override
    protected void setUpView() {
        etTitle = getContentView().findViewById(R.id.add_title);
        etDesc = getContentView().findViewById(R.id.add_desc);
        etNum = getContentView().findViewById(R.id.add_num);
        subBtn = getContentView().findViewById(R.id.add_submit);
    }

    @Override
    protected void setUpData() {
        //subBtn.setOnClickListener(new View.OnClickListener() {
        // lambda
        subBtn.setOnClickListener(view -> {
            HotelEntity hotelEntity = new HotelEntity();
            hotelEntity.setId(UUID.randomUUID().toString());
            hotelEntity.setTitle(String.valueOf(etTitle.getText()));
            hotelEntity.setDescription(String.valueOf(etDesc.getText()));
            hotelEntity.setNumber(Integer.parseInt(String.valueOf(etNum.getText())));
            boolean success = HotelDao.add(getWritableDb(), hotelEntity);
            if (success) {
                ((BaseActivity) getMContext()).navigateTo(ManagerActivity.class);
                ((BaseActivity) getMContext()).showToastSync("添加成功");
            } else {
                ((BaseActivity) getMContext()).navigateTo(ManagerActivity.class);
                ((BaseActivity) getMContext()).showToastSync("添加失败");
            }
        });
    }
}